#include <unistd.h>
#include <string>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <thread>
using namespace std;
int main(int argc, char *argv[], char *envp[])
{
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0)
    {
        cout << "socket error" << endl;
    }
    struct sockaddr_in server;
    bzero(&server, sizeof(server));
    server.sin_addr.s_addr = inet_addr(argv[1]);
    server.sin_family = AF_INET;
    server.sin_port = htons(atoi(argv[2]));
    // int n=connect(sock,(sockaddr*)&server,sizeof(server));
    int cnt = 5;
    while (0 != connect(sock, (struct sockaddr *)&server, sizeof(server)))
    {
        sleep(1);
        cerr << "正在重新链接，重连次数还有：" << cnt-- << endl;
        if (cnt <= 0)
        {
            break;
        }
    }
    int count = 0;
    while (1)
    {
        char buff[1024];

        sprintf(buff, "The number is %d", count++);
        write(sock, buff, strlen(buff));
        cout << "写入成功" << endl;
        sleep(1);
    }
    return 0;
}